package com.imist.jdbc.filter;


import com.imist.jdbc.bean.User;

import javax.servlet.*;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

public class UserFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        // 避免每一次的请求都要去获取user，直接在过滤器中写在请求域中
        User user = (User) ((HttpServletRequest)servletRequest).getSession().getAttribute("user");
        servletRequest.setAttribute("user",user);
        filterChain.doFilter(servletRequest,servletResponse);
    }

    @Override
    public void destroy() {

    }
}
